package com.servlet;

import com.dao.EmployeeDao;
import com.model.Employee;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/saveEmp")
public class EmpSaveServlet extends HttpServlet {
 private EmployeeDao employeeDao= new EmployeeDao();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");

        String name = req.getParameter("empName");
        String gender = req.getParameter("empGender");
        String salary = req.getParameter("empSalary");

        int row = employeeDao.insert(new Employee(null, name, gender, null, Double.parseDouble(salary)));
        if (row > 0) {
            req.setAttribute("msg", "宿舍添加成功");
        } else {
            req.setAttribute("msg", "宿舍添加失败");
        }
        req.getRequestDispatcher("msg.jsp").forward(req, resp);
    }
}
